package io.flutter.facade;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.taobao.idlefish.flutterboost.containers.BoostFlutterFragment;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.GeneratedPluginRegistrant;

public abstract class DKBaseFlutterFragment extends BoostFlutterFragment {
    private DkFlutterInterface proxy;

    public void setProxy(DkFlutterInterface proxy) {
        this.proxy = proxy;
    }

    @Override
    public void onRegisterPlugins(PluginRegistry registry) {
        GeneratedPluginRegistrant.registerWith(registry);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (null != proxy) {
            proxy.onCreateView(inflater, container, savedInstanceState, contentView);
        }
        return contentView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (null != proxy) {
            proxy.onViewCreated(view, savedInstanceState);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (null != proxy) {
            proxy.onActivityCreated(savedInstanceState);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (null != proxy) {
            proxy.onResume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (null != proxy) {
            proxy.onPause();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (null != proxy) {
            proxy.onStop();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != proxy) {
            proxy.onDestroyView();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != proxy) {
            proxy.onDestroy();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null != proxy) {
            proxy.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        if (null != proxy) {
            proxy.onSaveInstanceState(outState);
        }
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        if (null != proxy) {
            proxy.onViewStateRestored(savedInstanceState);
        }
    }

    /**
     * 字图标 变黑 全屏没有占位
     */
    protected void setDarkModeFullScreen() {
        if (null == getActivity() || null == getActivity().getWindow()) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            setMIUIStatusBarDarkIcon(true);
            setMeizuStatusBarDarkIcon(true);
            if (null != getActivity().getWindow().getDecorView()) {
                getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            }
        }
    }

    /**
     * 字图标 变白 全屏没有占位
     */
    public void setLightModeFullScreen() {
        if (null == getActivity() || null == getActivity().getWindow()) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            setMIUIStatusBarDarkIcon(false);
            setMeizuStatusBarDarkIcon(false);
            if (null != getActivity().getWindow().getDecorView()) {
                getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            }
        }
    }

    protected void setStatusBarColor(int color) {
        if (null == getActivity() || null == getActivity().getWindow()) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getActivity().getWindow().setStatusBarColor(color);
        } else {
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    /**
     * 修改 MIUI V6  以上状态栏颜色
     */
    private void setMIUIStatusBarDarkIcon(boolean darkIcon) {
        if (null == getActivity() || null == getActivity().getWindow()) {
            return;
        }
        Class<? extends Window> clazz = getActivity().getWindow().getClass();
        try {
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            int darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(getActivity().getWindow(), darkIcon ? darkModeFlag : 0, darkModeFlag);
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }

    /**
     * 修改魅族状态栏字体颜色 Flyme 4.0
     */
    private void setMeizuStatusBarDarkIcon(boolean darkIcon) {
        if (null == getActivity() || null == getActivity().getWindow()) {
            return;
        }
        try {
            WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
            Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
            Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
            darkFlag.setAccessible(true);
            meizuFlags.setAccessible(true);
            int bit = darkFlag.getInt(null);
            int value = meizuFlags.getInt(lp);
            if (darkIcon) {
                value |= bit;
            } else {
                value &= ~bit;
            }
            meizuFlags.setInt(lp, value);
            getActivity().getWindow().setAttributes(lp);
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }
}